"""
离线文档配置工具
为FastAPI配置本地静态资源，解决内网环境下无法加载CDN资源的问题
"""

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles


def setup_offline_docs(app: FastAPI):
    """配置离线文档"""

    # 挂载静态文件服务
    app.mount("/static", StaticFiles(directory="static"), name="static")

    # 自定义Swagger UI路由
    @app.get("/docs", include_in_schema=False)
    async def custom_swagger_ui_html():
        html_content = f"""
        <!DOCTYPE html>
        <html>
        <head>
            <title>{app.title} - Swagger UI</title>
            <link rel="stylesheet" type="text/css" href="/static/docs/swagger-ui.css" />
            <style>
                html {{ box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }}
                *, *:before, *:after {{ box-sizing: inherit; }}
                body {{ margin:0; background: #fafafa; }}
            </style>
        </head>
        <body>
            <div id="swagger-ui"></div>
            <script src="/static/docs/swagger-ui-bundle.js"></script>
            <script src="/static/docs/swagger-ui-standalone-preset.js"></script>
            <script>
                window.onload = function() {{
                    const ui = SwaggerUIBundle({{
                        url: '{app.openapi_url}',
                        dom_id: '#swagger-ui',
                        deepLinking: true,
                        presets: [
                            SwaggerUIBundle.presets.apis,
                            SwaggerUIStandalonePreset
                        ],
                        plugins: [
                            SwaggerUIBundle.plugins.DownloadUrl
                        ],
                        layout: "StandaloneLayout",
                        defaultModelsExpandDepth: -1
                    }});
                }};
            </script>
        </body>
        </html>
        """
        return HTMLResponse(content=html_content)

    # 自定义ReDoc路由
    @app.get("/redoc", include_in_schema=False)
    async def redoc_html():
        html_content = f"""
        <!DOCTYPE html>
        <html>
        <head>
            <title>{app.title} - ReDoc</title>
            <meta charset="utf-8"/>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <style>
                body {{ margin: 0; padding: 0; }}
            </style>
        </head>
        <body>
            <div id="redoc-container"></div>
            <script src="/static/docs/redoc.standalone.js"></script>
            <script>
                Redoc.init('{app.openapi_url}', {{}}, document.getElementById('redoc-container'));
            </script>
        </body>
        </html>
        """
        return HTMLResponse(content=html_content)


from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles


def setup_offline_docs(app: FastAPI):
    """配置离线文档"""

    # 挂载静态文件服务
    app.mount("/static", StaticFiles(directory="static"), name="static")

    # 自定义Swagger UI路由
    @app.get("/docs", include_in_schema=False)
    async def custom_swagger_ui_html():
        return HTMLResponse(
            """
<!DOCTYPE html>
<html>
<head>
    <title>{title} - Swagger UI</title>
    <link rel="stylesheet" type="text/css" href="/static/docs/swagger-ui.css" />
    <link rel="icon" type="image/png" href="/static/docs/favicon-32x32.png" sizes="32x32" />
    <link rel="icon" type="image/png" href="/static/docs/favicon-16x16.png" sizes="16x16" />
    <style>
        html {{
            box-sizing: border-box;
            overflow: -moz-scrollbars-vertical;
            overflow-y: scroll;
        }}
        *, *:before, *:after {{
            box-sizing: inherit;
        }}
        body {{
            margin:0;
            background: #fafafa;
        }}
    </style>
</head>
<body>
    <div id="swagger-ui"></div>
    <script src="/static/docs/swagger-ui-bundle.js"> </script>
    <script src="/static/docs/swagger-ui-standalone-preset.js"> </script>
    <script>
    window.onload = function() {{
        const ui = SwaggerUIBundle({{
            url: '{openapi_url}',
            dom_id: '#swagger-ui',
            deepLinking: true,
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],
            plugins: [
                SwaggerUIBundle.plugins.DownloadUrl
            ],
            layout: "StandaloneLayout"
        }})
    }}
    </script>
</body>
</html>
        """.format(
                title=app.title, openapi_url=app.openapi_url
            )
        )

    # 自定义ReDoc路由
    @app.get("/redoc", include_in_schema=False)
    async def redoc_html():
        return HTMLResponse(
            """
<!DOCTYPE html>
<html>
<head>
    <title>{title} - ReDoc</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
    <style>
        body {{
            margin: 0;
            padding: 0;
        }}
    </style>
</head>
<body>
    <redoc spec-url='{openapi_url}'></redoc>
    <script src="/static/docs/redoc.standalone.js"> </script>
</body>
</html>
        """.format(
                title=app.title, openapi_url=app.openapi_url
            )
        )
